package com.qfedu.microuser.oaLogin.controller;

import com.qfedu.common.redis.utils.RedisCache;
import com.qfedu.microuser.oaLogin.entity.Sys_user;
import com.qfedu.microuser.oaLogin.entity.LeaveForm;
import com.qfedu.common.core.common.Result;
import com.qfedu.microuser.oaLogin.entity.WorkOvertimeRequest;
import com.qfedu.microuser.oaLogin.entity.loginDTO;
import com.qfedu.microuser.oaLogin.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * --- 这有何难 ---
 * ƪ(˘⌣˘)ʃ
 *
 * @author 超级无敌张恒阳大王
 * @date 11/10/2025
 * @desc
 */
//@CrossOrigin
@RestController
@RequestMapping("api")
@Slf4j
public class MyLoginController {

	@Autowired
	private LoginService loginService;

	@Autowired
	private RedisCache redisCache;

	/**
	 * 登录接口
	 *
	 * @param loginDTO
	 * @return
	 */
	@PostMapping("/user/login")
	public Result login(@RequestBody loginDTO loginDTO) {
		log.info("接收到的登录请求: {}", loginDTO);
		Sys_user user = loginService.getPasswordByName(loginDTO);

		// 存入Redis
		redisCache.setCacheObject("user" + user.getUsername(), user, 30, TimeUnit.MINUTES);

		// 设置Result属性
		Result r = Result.success(user);
		r.setUsername(user.getUsername());

		return r;
	}

	/**
	 * 假的token校验
	 *
	 * @return
	 */
	@PostMapping("/user/check-token")
	public Result checkToken() {
		log.info("接收到checkToken请求");
		return Result.success();
	}

	/**
	 * 加班-->get-sn
	 *
	 * @return
	 */
	@PostMapping("/flow/get-sn")
	public Result get_sn() {
		log.info("调用get-sn接口");
		return Result.success("1001");
	}

	/**
	 * 加班-->add
	 *
	 * @param workOvertimeRequest
	 * @return
	 */
	@PostMapping("/flow/add")
	public Result addWorkOverTime(@RequestBody WorkOvertimeRequest workOvertimeRequest) {
		log.info("接收到的加班参数: {}", workOvertimeRequest);
		Map<String, String> data = new HashMap<>();
		data.put("id", "123456");
		return Result.success(data);
	}


	// 目前这个接口重复了
	@PostMapping("/flow/list")
	public Result get_list() {
		LeaveForm data = getLeaveForm();
		log.info("访问了get-list接口 :{}", data);
		return Result.success(data);
	}


	public static LeaveForm getLeaveForm() {
		LeaveForm leaveForm = new LeaveForm();

		// 填充表单ID
		leaveForm.set_id("leave_" + System.currentTimeMillis());

		// 随机选择请假类型：这里选择2表示年假
		leaveForm.setLeaveid(2);

		// 填充请假时长（年假按天算）
		leaveForm.setTotals("3");

		// 生成流水单号
		leaveForm.setSericnum("LEAVE" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "00123");

		// 填充请假事由
		leaveForm.setExplain("家庭事务处理");

		// 模块ID固定为2（请假模块）
		leaveForm.setModeid(2);

		// 填充备注信息
		leaveForm.setRemarks("处理家庭紧急事务，需要3天时间");

		// 设置审批状态为待审批
		leaveForm.setResult(30);

		// 审批意见暂时为空
		leaveForm.setChecksm("");

		// 设置开始时间（3天后）
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 3);
		leaveForm.setStart_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));

		// 设置结束时间（开始时间+3天）
		calendar.add(Calendar.DAY_OF_MONTH, 3);
		leaveForm.setEnd_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));

		// 填充用户信息
		leaveForm.setUser_id("user_001");
		leaveForm.setUsername("张小明");

		// 填充部门信息
		leaveForm.setDepartment_id("dept_hr");

		// 填充部门对象信息
		Map<String, Object> department = new HashMap<>();
		department.put("id", "dept_hr");
		department.put("name", "人力资源部");
		leaveForm.setDepartment(department);

		// 设置表单标题
		leaveForm.setTitle("张小明的请假申请");

		// 设置创建时间
		leaveForm.setCreate_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		return leaveForm;

	}

}
